home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 7523 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.6 KB

  1. Path: koobera.math.uic.edu!djb
  2. From: djb@koobera.math.uic.edu (D. J. Bernstein)
  3. Message-ID: <1996Feb2300.56.01.15905@koobera.math.uic.edu>
  4. Date: 23 Feb 1996 00:56:01 GMT
  5. Newsgroups: comp.arch.arithmetic,comp.lang.c,comp.lang.c++
  6. Subject: Re: Access carry flag from C
  7. References: <Dn1C9z.DGv.0.net@indra.com> <1996Feb1922.17.19.879@koobera.math.uic.edu> <4gihtk$33k@sun001.spd.dsccc.com>
  8. Organization: IR
  9.  
  10. Mike McCarty <jmccarty@spd.dsccc.com> wrote:
  11. > D. J. Bernstein <djb@koobera.math.uic.edu> wrote:
  12. > > A smart compiler will optimize ``if (i < j)''---I'm assuming here that
  13. > > all variables are unsigned---into a test of the carry bit.
  14. > Depends on which (if any) of i and j are unsigned whether that is even
  15. > the correct test.
  16.  
  17. Learn to read. ``I'm assuming here that all variables are unsigned.''
  18.  
  19. > Also, your statement actually depends on having complement arithmetic,
  20.  
  21. The correctness of ``if (i < j)'' as an overflow test is guaranteed by
  22. the ANSI C definition of unsigned arithmetic, which happens to match
  23. what every existing compiler does.
  24.  
  25. On all the CPUs I care about, and presumably all the CPUs that the
  26. original poster cares about, there's a carry bit equalling (i < j). A
  27. good compiler will recognize this; an excellent compiler will document
  28. that fact; a bad compiler will do an extra subtraction, which at least
  29. isn't painfully slow.
  30.  
  31.   [ signed arithmetic ]
  32. > This is a difficult problem, and such checks are going to be s-l-o-w.
  33.  
  34. There's no reason for C to be any slower than asm. If the compiler
  35. provides a documented idiom for each machine-language operation, you can
  36. achieve the speed of asm with no loss in portability.
  37.  
  38. ---Dan
  39.